/*
 * @Author: kanglang
 * @Date: 2020-06-28 12:55:05
 * @LastEditors: kanglang
 * @LastEditTime: 2021-06-08 19:11:48
 * @Description: 活体认证，备用
 */
import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Image,
  TouchableOpacity,
  Platform,
  NativeModules,
  NativeEventEmitter,
  DeviceEventEmitter,
  Dimensions,
} from 'react-native';
import { px2dp } from '../../common/styles';

let { height } = Dimensions.get('window');
let topHeight = null;
const FaceCheckHelper = Platform.select({
  android: () => NativeModules.PushFaceViewControllerModule,
  ios: () => NativeModules.RNIOSExportJsToReact,
})();
const NativeModule = new NativeEventEmitter(FaceCheckHelper);

export default class Live extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      idcard1: require('../../image/icons/idcard1.png'),
      isOk: false,
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.main}>
          <TouchableOpacity
            style={
              (styles.center,
              {
                width: px2dp(88),
                height: px2dp(88),
                backgroundColor: '#F0EDEB',
                borderRadius: 88,
                marginTop: px2dp(66),
              })
            }
            activeOpacity={1}
            onPress={this._toLive}
          >
            {/* 如果日后需要返回检验的内容，则将下面更换为<Image/> */}

            <View
              style={{ width: px2dp(88), height: px2dp(88), borderRadius: 88 }}
            >
              <Text>开始</Text>
            </View>
          </TouchableOpacity>
          <View style={styles.textWrap}>
            <Text style={styles.Text}>请将摄像头对准脸部按提示操作</Text>
            {this.state.isOk ? (
              <Image
                style={{ width: px2dp(14), height: px2dp(14), marginLeft: 4 }}
                source={require('../../image/icons/icon_success_green.png')}
              />
            ) : null}
          </View>
        </View>
      </View>
    );
  }

  _handleBack() {
    const { navigator } = this.props;
    // if (navigator && navigator.getCurrentRoutes().length > 1) {
    //     navigator.pop()
    //     return true;
    // }
    // navigator.replace({
    //   component: CertificationProgress
    // });
    return false;
  }

  onLayout = (event) => {
    topHeight = event.nativeEvent.layout.height;
  };

  _toLive = () => {
    let obj = {
      // 质量校验设置
      quality: {
        minFaceSize: 200, // 设置最小检测人脸阈值 默认是200
        cropFaceSizeWidth: 400, // 设置截取人脸图片大小 默认是 400
        occluThreshold: 0.5, // 设置人脸遮挡阀值 默认是 0.5
        illumThreshold: 40, // 设置亮度阀值 默认是 40
        blurThreshold: 0.7, // 设置图像模糊阀值 默认是 0.7
        EulurAngleThrPitch: 10, // 设置头部姿态角度 默认是 10
        EulurAngleThrYaw: 10, // 设置头部姿态角度 默认是 10
        EulurAngleThrRoll: 10, // 设置头部姿态角度 默认是 10
        isCheckQuality: true, // 设置是否进行人脸图片质量检测 默认是 true
        conditionTimeout: 10, // 设置超时时间 默认是 10
        notFaceThreshold: 0.6, // 设置人脸检测精度阀值 默认是0.6
        maxCropImageNum: 1, // 设置照片采集张数 默认是 1
      },
      liveActionArray: [
        // 0, //眨眨眼
        1, // 张张嘴
        // 2, //向右摇头
        // 3, //向左摇头
        // 4, //抬头
        // 5, //低头
        // 6, //摇头
      ], // 活体动作列表
      order: false, // 是否按顺序进行活体动作
      sound: true, // 提示音，默认不开启
    };
    FaceCheckHelper.openPushFaceViewController(obj);
    // 如果都不设置，需要传 {} 空对象， 建议设置 liveActionArray
    // FaceCheckHelper.openPushFaceViewController( {} );
  };

  faceCheckCallback(data) {
    console.log('--识别到的内容--', data);
    if (data.images.bestImage) {
    }
    let source = {
      uri: `data:image/jpeg;base64,${data.images.bestImage}`,
    };
    this.setState({
      idcard1: source,
      isOk: true,
    });
  }

  componentDidMount() {
    this._toLive();
    if (Platform.OS === 'android') {
      NativeModule.addListener('FaceCheckHelper', (data) => this.faceCheckCallback(data));
    } else {
      this.addListenerIOS();
    }
  }

  componentWillUnmount() {
    if (this.listener) {
      this.listener && this.listener.remove();
    }
  }

  // ios人脸检测回调
  addListenerIOS = () => {
    const { GJCommonEmitter } = NativeModules;
    const commonManagerEmitter = new NativeEventEmitter(GJCommonEmitter);
    this.listener = commonManagerEmitter.addListener(
      'FaceCheckHelper',
      (dataJsonStr) => {
        const { originalImageEncryptStr } = JSON.parse(dataJsonStr);
        // originalImageEncryptStr base64 图片
      },
    );
  };
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    // justifyContent:'center',
    // alignItems:'center',
    backgroundColor: '#F5F4F2',
    height: '100%',
  },
  main: {
    backgroundColor: '#fff',
    borderTopRightRadius: 12,
    borderTopLeftRadius: 12,
    marginTop: 16,
    height: height - topHeight - 16,
    alignItems: 'center',
  },
  text: {
    fontSize: 30,
    color: 'black',
  },
  center: {
    // justifyContent:'center',
    width: '100%',
    marginTop: 30,
    margin: 'auto',
    alignItems: 'center',
    // backgroundColor:'red',
    // height:300,
  },
  Image: {
    width: px2dp(260),
    maxWidth: 300,
    height: px2dp(180),
    // margin:'auto',
    marginBottom: 16,
    // marginTop:10
  },
  textWrap: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    marginTop: px2dp(34),
  },
  Text: {
    color: '#333232',
  },
  button: {
    color: '#fff',
    padding: 12,
    width: '80%',
    height: 48,
    borderRadius: 8,
    backgroundColor: 'red',
    marginTop: 20,
    alignItems: 'center',
  },

  white: {
    color: '#fff',
    textAlign: 'center',
  },
});
